<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}AI Chat 对话`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="用户Id" prop="userId">
        <el-input v-model.trim="drawerProps.row!.userId" placeholder="请填写用户Id" clearable></el-input>
      </el-form-item>
      <el-form-item label="对话标题" prop="title">
        <el-input v-model.trim="drawerProps.row!.title" placeholder="请填写对话标题" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否置顶" prop="pinned">
        <el-input v-model.trim="drawerProps.row!.pinned" placeholder="请填写是否置顶" clearable></el-input>
      </el-form-item>
      <el-form-item label="置顶时间" prop="pinnedTime">
        <el-input v-model.trim="drawerProps.row!.pinnedTime" placeholder="请填写置顶时间" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色id" prop="roleId">
        <el-input v-model.trim="drawerProps.row!.roleId" placeholder="请填写角色id" clearable></el-input>
      </el-form-item>
      <el-form-item label="知识库id" prop="knowledgeId">
        <el-input v-model.trim="drawerProps.row!.knowledgeId" placeholder="请填写知识库id" clearable></el-input>
      </el-form-item>
      <el-form-item label="模型id" prop="modelId">
        <el-input v-model.trim="drawerProps.row!.modelId" placeholder="请填写模型id" clearable></el-input>
      </el-form-item>
      <el-form-item label="角色设定" prop="systemMessage">
        <el-input v-model.trim="drawerProps.row!.systemMessage" placeholder="请填写角色设定" clearable></el-input>
      </el-form-item>
      <el-form-item label="温度参数" prop="temperature">
        <el-input v-model.trim="drawerProps.row!.temperature" placeholder="请填写温度参数" clearable></el-input>
      </el-form-item>
      <el-form-item label="单条回复的最大 Token 数量" prop="maxTokens">
        <el-input v-model.trim="drawerProps.row!.maxTokens" placeholder="请填写单条回复的最大 Token 数量" clearable></el-input>
      </el-form-item>
      <el-form-item label="上下文的最大 Message 数量" prop="maxContexts">
        <el-input v-model.trim="drawerProps.row!.maxContexts" placeholder="请填写上下文的最大 Message 数量" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="ChatDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { Chat } from "@/api/ai/chat";
const rules = reactive({
  userId: [{ required: true, message: "请填写用户Id" }],
  title: [{ required: true, message: "请填写对话标题" }],
  pinned: [{ required: true, message: "请填写是否置顶" }],
  pinnedTime: [{ required: true, message: "请填写置顶时间" }],
  roleId: [{ required: true, message: "请填写角色id" }],
  knowledgeId: [{ required: true, message: "请填写知识库id" }],
  modelId: [{ required: true, message: "请填写模型id" }],
  systemMessage: [{ required: true, message: "请填写角色设定" }],
  temperature: [{ required: true, message: "请填写温度参数" }],
  maxTokens: [{ required: true, message: "请填写单条回复的最大 Token 数量" }],
  maxContexts: [{ required: true, message: "请填写上下文的最大 Message 数量" }],
  createTime: [{ required: true, message: "请填写创建时间" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Chat.ReqChat>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}AI Chat 对话成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
